<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2020/9/23
 * Time: 10:20
 */

namespace ticket\modules\backend\controllers;


use ticket\libs\Tools;
use ticket\modules\backend\models\CouponConfig;

/*** 优惠券
 * Class CouponController
 * @package ticket\modules\backend\controllers
 */
class CouponController extends BasicController
{

    /*** 优惠券列表
     * @return false|string
     */
    public function actionList()
    {
        $get = \Yii::$app->request->get();
        $page = isset($get['page'])?$get['page']:1;
        $pageSize = isset($get['size'])?$get['size']:10;

        $aid = $this->appId;
        if (empty($aid)){
            return Tools::showRes(10007,'请登录');
        }

        $searchModel = CouponConfig::find()->where(['app_id' => $aid]);
        $count = $searchModel->count();
        $couponList = $searchModel->offset($pageSize * ($page - 1))->orderBy(['cid' => SORT_DESC])->asArray()->limit($pageSize)->all();

        return Tools::showRes(0,'success',[
            'couponList' => $couponList,
            'page' => $page,
            'size' => $pageSize,
            'count' => $count,
        ]);
    }


    /*** 获取应用中心的优惠券模板
     * @return false|string
     */
    public function actionGetTemplate()
    {
        $app_id = $this->appId;

        $templateList = '';




        return Tools::showRes(0,'success',[
            'templateList' => $templateList,
        ]);

    }
    
    
    public function actionAdd()
    {
        if (\Yii::$app->request->isPost){
            $post = \Yii::$app->request->post();
            $app_id = $this->appId;
            $couponModel = new CouponConfig();
            if ($couponModel->addCoupon($app_id,$post)){
                return Tools::showRes(0,'success');
            }else{
                if ($couponModel->hasErrors()){
                    return Tools::showRes(10405,$couponModel->getErrors());
                }else{
                    return Tools::showRes(10406,'添加失败');
                }
            }
        }
    }


    /*** 获取单个优惠券信息
     * @return false|string
     */
    public function actionInfo()
    {
        $get = \Yii::$app->request->get();
        $id = isset($get['cid'])?$get['cid']:'';
        if (empty($id)){
            return Tools::showRes(10404,'参数有误');
        }

        $couponInfo = CouponConfig::find()->where('cid = :id',[':id' => $id])->asArray()->one();

        return Tools::showRes(0,'success',[
            'couponInfo' => $couponInfo,
        ]);
    }


    /*** 删除优惠券信息
     * @return false|string
     */
    public function actionDel()
    {
        if (\Yii::$app->request->isPost){
            $post = \Yii::$app->request->post();
            $id = isset($post['cid'])?$post['cid']:'';
            $app_id = $this->appId;
            if (empty($id)){
                return Tools::showRes(10404,'参数有误');
            }

            $couponModel = new CouponConfig();
            if ($couponModel->delInfo($app_id,$id)){
                return Tools::showRes(0,'删除成功');
            }
            return Tools::showRes(10405,'删除失败');
        }
    }

}